package com.one.common.common.user.modle;

import com.one.common.config.CMemoryData;
import com.one.common.model.bean.CityBean;
import com.one.common.model.bean.UserInfoBean;
import com.one.common.model.http.base.BaseModel;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.utils.ImageFactory;
import com.one.common.view.base.BaseActivity;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/8/5 下午5:43
 * description ：
 **/
public class UserInfoModel extends BaseModel<UserInfoApi> {
    public UserInfoModel(BaseActivity mActivity) {
        super(UserInfoApi.class, mActivity);
    }

    /**
     * 获取用户信息
     */
    public void getUserInfo(ObserverOnResultListener<UserInfoBean> listener){
        handleOnResultObserver(mApiService.getUserInfo(getParams()), listener);
    }


    /**
     * 更新用户信息
     */
    public void updateUserInfo(Map<String , String> params, ObserverOnResultListener<String> listener){
        params.putAll(getParams());
        handleOnResultObserver(mApiService.updateUserInfo(params), listener);
    }

    /**
     * 上传图片
     */
    public void uploadImg(File file, ObserverOnResultListener<String> listener){
        //构建body
        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file))
                .build();
        handleOnResultObserver(mApiService.uploadImg(requestBody), listener);

    }

    /**
     * 获取城市列表
     */
    public void getCitys(ObserverOnResultListener<ArrayList<CityBean>> listener){
        handleOnResultObserver(mApiService.getCitys(getParams()), listener);
    }

    /**
     * 意见反馈
     */
    public void feedback(String feedback, ObserverOnResultListener<String> listener){
        Map<String,String> params = getParams();
        params.put("advice_desc",feedback);
        handleOnResultObserver(mApiService.feedback(params), listener);
    }

    /**
     * 意见反馈
     */
    public void getYouhuijuan( ObserverOnResultListener<String> listener){
        UserInfoBean bean = CMemoryData.getUserInfo();
        Map<String,String> params = getParams();
        params.put("member_account",bean.getMember_account());
        handleOnResultObserver(mApiService.getYouhuijuan(params), listener);
    }

    /**
     * 退出登录
     */
    public void logout(ObserverOnResultListener<Object> listener){
        handleOnResultObserver(mApiService.logout(getParams()), listener);
    }


    /**
     * 上传多张图片
     */
    public void uploadImgs(final ArrayList<String> paths, final ObserverOnResultListener<ArrayList<String>> listener){
        new Thread(new Runnable() {
            @Override
            public void run() {
                //构建body
                MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
                File file;
                for (String p : paths){
                    file = getImageFile(p);
                    builder.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
                }
                RequestBody requestBody = builder.build();
                handleOnResultObserver(mApiService.uploadImgs(requestBody), listener);
            }
        }).start();


    }

    /**
     * 缩放图片
     */
    private File getImageFile(String path){
        String outFilePath = mActivity.getExternalCacheDir().getPath() + System.currentTimeMillis() + ".jpg";
        ImageFactory imageFactory = new ImageFactory();
        try {
            imageFactory.ratio(imageFactory.getBitmap(path), 480, 480, outFilePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return new File(outFilePath);
    }
}
